package org.csu.jpetstoreagain.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.csu.jpetstoreagain.domain.*;
import org.csu.jpetstoreagain.persistence.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.sound.midi.InvalidMidiDataException;
import java.sql.Wrapper;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

@Service
public class OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private LineItemMapper lineItemMapper;
    @Autowired
    private SequenceMapper sequenceMapper;
    @Autowired
    private InventoryMapper inventoryMapper;
    @Autowired
    private OrderStatusMapper orderStatusMapper;

    public void insertOrder(Order order) throws InvalidMidiDataException {
        orderMapper.insert(order);
    }

    public void insertLineItems(List<LineItem> lineItems){
        for(LineItem lineItem:lineItems){
            lineItemMapper.insert(lineItem);
        }
    }

    public Order getOrder(int orderId) {
        return orderMapper.getOrder(orderId);
    }

    public List<LineItem> getLineItemsByOrderId(int orderId){
        QueryWrapper<LineItem> wrapper = new QueryWrapper<>();
        wrapper.eq("orderid",orderId);
        List<LineItem>lineItems = lineItemMapper.selectList(wrapper);
        return lineItems;
    }

    public List<Order> getOrdersByUserid(String userid) {
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("userid",userid);
        List<Order>orders = orderMapper.selectList(wrapper);
        return orders;
    }

    //其实是获取下一个订单编号的
    public int getNextId(String name) throws InvalidMidiDataException {
        QueryWrapper<Sequence> wrapper = new QueryWrapper<>();
        wrapper.eq("name",name);
        Sequence sequence = sequenceMapper.selectOne(wrapper);
        //拿到下一个订单编号
        int nextId = sequence.getNextId();

        //提交下下个订单编号 用于下一次获取(当前的逻辑只是+1）
        sequence.setNextId(sequence.getNextId()+1);
        sequenceMapper.update(sequence,wrapper);

        //返回下个订单编号
        return nextId;
    }

    //减少库存
    public int inventoryByItemId(String itemId,int size){
        QueryWrapper<Inventory> wrapper = new QueryWrapper<>();
        wrapper.eq("itemid",itemId);
        Inventory inventory = inventoryMapper.selectOne(wrapper);
        int left = inventory.getQty() - size;
        inventory.setQty(left);
        inventoryMapper.update(inventory,wrapper);
        //返回剩余库存量
        return left;
    }

    public void insertOrderStatus(Order order){
        QueryWrapper<OrderStatus> wrapper = new QueryWrapper<>();
        wrapper.eq("orderid",order.getOrderId());
        OrderStatus orderStatus = new OrderStatus();
        orderStatus.setOrderid(order.getOrderId());
        orderStatus.setLinenum(getLineItemsByOrderId(order.getOrderId()).size());
        orderStatus.setStatus("P");
        orderStatus.setTimestamp(new Date());
        orderStatusMapper.insert(orderStatus);
    }

    public Boolean isItemLack(String itemId,int size){
        QueryWrapper<Inventory> wrapper = new QueryWrapper<>();
        wrapper.eq("itemid",itemId);
        Inventory inventory = inventoryMapper.selectOne(wrapper);
        if(inventory.getQty()-size>0){
            return false;
        }else {
            return true;
        }
    }
}
